//
//  GMHButtonViewController.m
//  
//
//  Created by 郭明皓 on 15/10/24.
//
//

#import "GMHButtonViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "GMHActivity.h"
#import "GMHButtonCell.h"
#import "GMHSecondViewController.h"
#import "GMHDataFiled.h"
#import "DKNightVersion.h"

@interface GMHButtonViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *ButtonArry;
@property (nonatomic, assign)int pageIndex;
@property (nonatomic, assign)BOOL isUpPull;
@end

@implementation GMHButtonViewController
- (void)dealloc
{
    [_tableView release];
    [_ButtonArry release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 200;
    self.tableView.nightBackgroundColor = [UIColor blackColor];
    self.tableView.nightSeparatorColor = [UIColor darkGrayColor];

    self.ButtonArry = [NSMutableArray array];


    [self DownPull];
    [self UpPull];
}
- (void)DownPull
{
    __block GMHButtonViewController *find = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        self.isUpPull = NO;
        [find createData];
    }];
    [self.tableView.header beginRefreshing];
}
- (void)UpPull
{
    __block GMHButtonViewController *find = self;
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self.tableView.footer endRefreshing];
        self.pageIndex++;
        self.isUpPull = YES;
        [find createData];
    }];
//    [self.tableView.footer beginRefreshing];
}
- (void)createData
{
    [GMHDataFiled isHaveFileDocument];
    [GMHDataFiled isHaveFileActivity];

    NSString *strurl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=99000505125188&track_app_version=6.8.1&track_app_channel=xiaomi&track_device_info=aries&track_os=Android5.0.2&app_installtime=1445388019292&lat=38.883536&lon=121.544393&page=%d&count=10", _pageIndex];
    if (_isUpPull == NO) {
        [self.ButtonArry removeAllObjects];
    }
    
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    [manager GET:strurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSMutableArray *arr = [GMHActivity baseModelArry:dic[@"data"]];
        [self.ButtonArry addObjectsFromArray:arr];
        NSLog(@"sssss%@", arr);
        if (_ButtonArry.count > 0) {
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
        }
        
            NSLog(@"%ld", self.ButtonArry.count);
        
        NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Activity/Button.plist"];
        [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];
        
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败");
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GMHSecondViewController *sec = [[GMHSecondViewController alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
    [sec release];
    
    GMHActivity *act = self.ButtonArry[indexPath.row];
    sec.url = act.url;
    sec.titles = act.title;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ButtonArry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strs = @"tableView";
    GMHButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
    if (!cell) {
        cell = [[GMHButtonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strs];
    }
    GMHActivity *act = self.ButtonArry[indexPath.row];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:act.photo]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
    return cell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
